package org.wzy.xmlg_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wzy.xmlg_backend.entity.Result;


import org.wzy.xmlg_backend.entity.Class;
import org.wzy.xmlg_backend.service.IClassService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author 位置杨
 * @since 2025-01-27
 */
@RestController
@Tag(name = "ClassController", description = "班级表")
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private IClassService classService;

    /**
     * 获取所有班级信息
     * 
     * @return 班级列表
     */
    @GetMapping
    @Operation(summary = "获取所有班级")
    public Result<List<Class>> getAllClasses() {
        List<Class> classList = classService.list();
        return Result.success(classList);
    }

    /**
     * 分页查询班级信息
     * 
     * @param current 当前页码
     * @param size 每页大小
     * @param name 班级名称（可选）
     * @param grade 年级（可选）
     * @return 分页结果
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询班级")
    public Result<Page<Class>> getClassPage(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "班级名称") @RequestParam(required = false) String name,
            @Parameter(description = "年级") @RequestParam(required = false) String grade) {
        
        Page<Class> page = new Page<>(current, size);
        LambdaQueryWrapper<Class> queryWrapper = new LambdaQueryWrapper<>();
        
        if (name != null && !name.trim().isEmpty()) {
            queryWrapper.like(Class::getName, name);
        }
        if (grade != null && !grade.trim().isEmpty()) {
            queryWrapper.like(Class::getGrade, grade);
        }
        
        Page<Class> result = classService.page(page, queryWrapper);
        return Result.success(result);
    }

    /**
     * 根据ID获取班级信息
     * 
     * @param id 班级ID
     * @return 班级信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取班级")
    public Result<Class> getClassById(@PathVariable Integer id) {
        Class classEntity = classService.getById(id);
        if (classEntity != null) {
            return Result.success(classEntity);
        } else {
            return Result.fail("班级不存在");
        }
    }

    /**
     * 创建新班级
     * 
     * @param classEntity 班级信息
     * @return 创建结果
     */
    @PostMapping
    @Operation(summary = "创建班级")
    public Result<Class> createClass(@RequestBody Class classEntity) {
        if (classEntity.getName() == null || classEntity.getName().trim().isEmpty()) {
            return Result.fail("班级名称不能为空");
        }
        if (classEntity.getGrade() == null || classEntity.getGrade().trim().isEmpty()) {
            return Result.fail("年级不能为空");
        }
        
        classEntity.setCreateTime(LocalDateTime.now());
        classEntity.setUpdateTime(LocalDateTime.now());
        
        boolean success = classService.save(classEntity);
        if (success) {
            return Result.success(classEntity);
        } else {
            return Result.fail("创建班级失败");
        }
    }

    /**
     * 更新班级信息
     * 
     * @param id 班级ID
     * @param classEntity 班级信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新班级")
    public Result<Class> updateClass(@PathVariable Integer id, @RequestBody Class classEntity) {
        Class existingClass = classService.getById(id);
        if (existingClass == null) {
            return Result.fail("班级不存在");
        }
        
        if (classEntity.getName() == null || classEntity.getName().trim().isEmpty()) {
            return Result.fail("班级名称不能为空");
        }
        if (classEntity.getGrade() == null || classEntity.getGrade().trim().isEmpty()) {
            return Result.fail("年级不能为空");
        }
        
        classEntity.setId(id);
        classEntity.setUpdateTime(LocalDateTime.now());
        
        boolean success = classService.updateById(classEntity);
        if (success) {
            return Result.success(classEntity);
        } else {
            return Result.fail("更新班级失败");
        }
    }

    /**
     * 删除班级
     * 
     * @param id 班级ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除班级", description = "根据ID删除班级")
    public Result<Void> deleteClass(@PathVariable Integer id) {
        boolean exists = classService.getById(id) != null;
        if (!exists) {
            return Result.fail("班级不存在");
        }
        
        boolean success = classService.removeById(id);
        if (success) {
            return Result.success();
        } else {
            return Result.fail("删除班级失败");
        }
    }
}